<template>
  <div class="modle parts" v-show="show">
    <div class="tit"><b>分类导航</b></div>
    <p class="info">导航样式：</p>
    <div class="box">
      <div
        :class="[{ on: styleNum == 1 }, ' css_box flex-column']"
        @click="chooseStyle('parts', 1)"
      >
        <div class="css css1">
          <span class="block" v-for="i in 5" :key="i"></span>
        </div>
        <div class="flex-center">样式一</div>
      </div>
      <div
        :class="[{ on: styleNum == 2 }, ' css_box flex-column']"
        @click="chooseStyle('parts', 2)"
      >
        <div class="css css2">
          <span class="block" v-for="i in 10" :key="i"></span>
        </div>
        <div class="flex-center">样式二</div>
      </div>
      <div
        :class="[{ on: styleNum == 3 }, ' css_box flex-column']"
        @click="chooseStyle('parts', 3)"
      >
        <div class="css css1">
          <span class="block" v-for="i in 5" :key="i"></span>
        </div>
        <div class="flex-center">
          <div class="active-wrap round"></div>
          <div class="round"></div>
        </div>
        <div class="flex-center">样式三</div>
      </div>
      <div
        :class="[{ on: styleNum == 4 }, ' css_box flex-column']"
        @click="chooseStyle('parts', 4)"
      >
        <div class="css css2">
          <span class="block" v-for="i in 10" :key="i"></span>
        </div>
        <div class="flex-center">
          <div class="active-wrap round"></div>
          <div class="round"></div>
        </div>
        <div class="flex-center">样式四</div>
      </div>
    </div>
    <p class="info">导航设置：</p>
    <div class="content">
      <div
        class="list"
        v-for="(item, index) in partList"
        :key="index"
      >
        <div :class="['left', { 'border-error' : item.error}]">
          <div class="upload">
            <div class="img_box">
              <div
                class="change_img flex-center"
                @click="changeUploadType(index)"
              >
                更换图片
              </div>
              <img class="img" :src="item.icon_dispaly" />
            </div>
          </div>
          <div class="select_box flex-column">
            <p class="label">跳转内容：</p>
            <el-cascader
              v-model="item.navid_array"
              :options="options"
              :props="{ expandTrigger: 'hover', value: 'id', label: 'name' }"
              @change="selectContent($event, index)"
            >
            </el-cascader>
          </div>
          <div class="error-msg" v-if="item.error">{{ item.errorMsg }}</div>
        </div>
        <div
          class="sort flex-column"
          :style="
            index == 0 || index == dataList.length - 1
              ? 'justify-content:center;'
              : ''
          "
        >
          <img
            v-show="index != 0"
            @click="Move(index, 'up')"
            src="@/assets/images/shop_up.png"
          />
          <img
            v-show="index != dataList.length - 1"
            @click="Move(index, 'down')"
            src="@/assets/images/shop_down.png"
          />
        </div>
      </div>
    </div>
    <div class="flex-center">
      <div class="submit flex-center" @click="submit()">保存</div>
    </div>
  </div>
</template>

<script>
import { packApi } from "@/utils/public";
import { partnerSet_navList, partnerSet_editNav } from "@/api/shop/shop";

export default {
  name: "parts",
  props: ["show", "styleNum", "puzzleImg", "upload_img", "uploadHeader"],
  data() {
    return {
      options: [],
      value: "",
      dataList: [
        { id: "", nav_id: "", type: "", ordid: "", icon: "" }, //id,type:菜单为2其余为1，orderId：排序
      ],
      upload_inedx: "",
      childeseNum: ['一', '二', '三', '四']
    };
  },
  computed: {
    partList() {
      const typeList = {
				1: 5,
				2: 10,
				3: 10,
				4: 20
			}
      const numToSlice = typeList[this.styleNum] || 5;  // 获取对应的截取长度
      // 如果 dataList 的长度少于 numToSlice，就填充到 numToSlice 长度
      while (this.dataList.length < numToSlice) {
        this.dataList.push({ 
          id: "",
          nav_id: "", 
          type: "", 
          ordid: "", 
          icon: "", 
          navid_array: [], 
          icon_dispaly: "",
          error: false,
          errorMsg: "",
        });  // 使用 push 来添加默认值，这会触发响应式
      }
      return this.dataList.slice(0, numToSlice);
    },
  },
  created() {
    let params = packApi({ type: 1 });
    partnerSet_navList(params).then((res) => {
      if (res.errcode == 0) {
        this.options = res.data.nav_default;
        this.$emit("chooseStyle", "parts", res.data.nav_num);
        this.dataList = res.data.nav_list.map((item) => {
          if (item.type == 2) {
            item.navid_array = [-1, item.nav_id];
          } else {
            item.navid_array = [item.nav_id];
          }
          item.error = false;
          item.errorMsg = "";
          return item;
        });
      } else {
        this.$message.error(res.errmsg);
      }
    });
  },
  methods: {
    chooseStyle(name, style) {
      this.$emit("chooseStyle", name, style);
    },
    //移动排序
    Move(index, type) {
      let toIndex;
      type == "up" ? (toIndex = index - 1) : (toIndex = index + 1);
      let arr_temp = [].concat(this.dataList); //创建一个新的临时数组，用以操作后不变更原数组
      arr_temp.splice(toIndex, 0, arr_temp.splice(index, 1)[0]); //在toIndex位置插入从index位置截取的元素
      this.dataList = arr_temp;
    },
    //选择跳转内容
    selectContent(data, index) {
      if (data.length == 2) {
        //选择的是商品分类
        this.dataList[index].type = 2;
      } else {
        this.dataList[index].type = 1;
      }
      this.dataList[index].navid_array = data;
    },
    uploadSuccess(res, index) {
      if (res.code == 0) {
        this.dataList[index].icon = res.data.path;
        this.dataList[index].icon_dispaly = res.data.src;
      }
    },
    uploadErr() {
      this.$message.error("上传失败，请重试");
    },
    submit() {
      let errorFound = false;
      for(let item of this.partList) {
        // 重置 error 和 errorMsg
        item.error = false;
        item.errorMsg = "";
         // 如果之前已经找到错误，直接退出循环
        if(errorFound) return;
        // 检查请选择跳转内容是否为空
        if(!item.navid_array.length) {
          item.error = true;
          item.errorMsg = "请选择跳转内容";
          errorFound = true; // 标记错误，停止继续检查后续项
        } else if(!item.icon_dispaly) {
          // 检查图片是否添加
          item.error = true;
          item.errorMsg = "请添加图片";
          errorFound = true; // 标记错误，停止继续检查后续项
        }
      }
      // 如果找到错误，直接返回
      if(errorFound) return;
      let list = this.partList.map((item, index) => {
        return {
          id: item.id,
          nav_id:
            item.navid_array.length > 1
              ? item.navid_array[1]
              : item.navid_array[0],
          type: item.type,
          ordid: index,
          icon: item.icon,
        };
      });
      let params = {
        nav_num: this.styleNum,
        list: JSON.stringify(list),
      };
      partnerSet_editNav(packApi(params)).then((res) => {
        if (res.errcode == 0) {
          this.$message.success("修改成功");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 打开父组件的图片上传方式
    changeUploadType(index) {
      this.upload_inedx = "";
      if (index !== "") {
        this.upload_inedx = index;
      }
      this.$emit("changeUploadType", {
        pid: 3,
        ref: "refParts",
      });
    },
    // 获取父级返回的图片信息
    getUploadImg(res) {
      console.log(res);
      let index = this.upload_inedx;
      this.uploadSuccess(res, index);
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/.el-input__inner {
  background: #e1f3f0;
  border-color: #19b098;
}
.parts {
  .info {
    margin-bottom: 30px;
  }
  .box {
    max-width: 580px;
    flex-wrap: wrap;
    margin-bottom: 20px;
  }
  .css_box {
    width: 248px;
    height: 158px;
    border: 1px solid #d1d1d1;
    background: #fff;
    justify-content: center;
    &.on {
      border-color: #19b098;
    }
    &:nth-of-type(2n) {
      margin-left: 20px;
    }
    &:nth-of-type(n+3) {
      margin-top: 8px;
    }
    .css {
      width: 100%;
      height: 100px;
      padding: 0 10px;
      .block {
        background: #03a98e;
        width: 40px;
        height: 40px;
      }
    }
    .css1 {
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
    .css2 {
      padding: 0 10px;
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      justify-content: space-around;
    }
    .round {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      border: 1px solid #03A98E;
      margin-bottom: 10px;
      &.active-wrap {
        background:#03A98E;
        margin-right: 20px;
      }
    }
  }
  .content {
    .list {
      display: flex;
      margin-bottom: 10px;
      .left {
        display: flex;
        max-width: 400px;
        flex: 1;
        background: #fff;
        padding: 23px 20px;
        border: 1px solid transparent;
        .img_box {
          margin-right: 18px;
          position: relative;
          .img {
            width: 86px;
            height: 86px;
            border: 1px solid #eaeaea;
            box-sizing: border-box;
          }
          .change_img {
            // width: 85px;
            height: 20px;
            background: #19b098;
            color: #fff;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            cursor: pointer;
          }
        }
        .select_box {
          .label {
            margin-bottom: 10px;
          }
        }
      }
      .sort {
        justify-content: space-around;
        margin-left: 20px;
      }
      .border-error {
        position: relative;
        border-color: #ff0000;
        margin-bottom: 20px;
      }
      .error-msg {
        position: absolute;
        bottom: -20px;
        left: 0;
        color: #ff0000;
        font-size: 12px;
      }
    }
  }
}
.submit {
  cursor: pointer;
}
</style>